#!/usr/bin/env bash

MAIN_FOLDER="$1"
if [ -z "${MAIN_FOLDER}" ]; then
    echo "WARN: Please appoint project name."
    exit 1
fi

# 源代码目录是否正确
SOURCE_DIR="src/${MAIN_FOLDER}"
if [ ! -e "${SOURCE_DIR}" ]; then
    echo "ERR: Source code dir not exist."
    exit 1
fi

# 目录编译地址是否正确
TARGET_BIN_DIR="output/${MAIN_FOLDER}/bin"
if [ ! -e "${TARGET_BIN_DIR}" ]; then
    echo "fTarget bin dir not exist."
    exit 1
fi

eval "cd ${SOURCE_DIR}"

export GO111MODULE=on
export GOPROXY=https://goproxy.cn

# 编译时执行
eval "go mod tidy"
if [ $? -ne 0 ];then
  echo "ERR: go mod tidy"
  exit 1
fi

eval "go mod download"
if [ $? -ne 0 ];then
  echo "ERR: go mod download"
  exit 1
fi

eval "go build -o ../../${TARGET_BIN_DIR} ./..."
if [ $? -ne 0 ];then
  echo "ERR: go build -o ../../${TARGET_BIN_DIR} ./..."
  exit 1
fi

echo 'finished'
